package com.example.monitordemo.util;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSON;
import lombok.experimental.UtilityClass;

import java.util.Date;

import static com.example.monitordemo.config.MonitorConfig.WINDOWS_INSTANCE;
import static com.example.monitordemo.constant.CommonConstant.API_PREFIX;
import static com.example.monitordemo.constant.CommonConstant.INSTANT_QUERY_PREFIX;
import static com.example.monitordemo.constant.WinConstant.*;

/**
 * windows监控工具类
 */
@UtilityClass
public class WinMonitorUtil {
    /**
     * 查询语句
     */
    String promQL;



    /**
     * windows信息概览
     * @return
     */
    public JSON getWindowsOverView(Date time,String instance){
        return getJson(time,instance,WINDOWS_OVERVIEW);
    }

    /**
     * 各主机cpu使用率
     * @return
     */
    public JSON getCpuUseRate(Date time,String instance){
        return getJson(time,instance,CPU_USE_RATE);
    }

    /**
     * 各主机内存使用率
     * @return
     */
    public JSON getMemoryUseRate(Date time,String instance){
        return getJson(time,instance,MEMORY_USE_RATE);
    }

    /**
     * 各主机最大流量网卡网络详情-发送
     * @return
     */
    public JSON getNetMaxSentDetail(Date time,String instance){
        return getJson(time,instance,NET_MAX_SENT_DETAIL);
    }

    /**
     * 各主机最大流量网卡网络详情-接收
     * @return
     */
    public JSON getNetMaxReceivedDetail(Date time,String instance){
        return getJson(time,instance,NET_MAX_RECEIVED_DETAIL);
    }

    /**
     * 各主机最大读详情
     * @return
     */
    public JSON getDiskMaxReadDetail(Date time,String instance){
        return getJson(time,instance,DISK_MAX_READ_DETAIL);
    }

    /**
     * 各主机最大写详情
     * @return
     */
    public JSON getDiskMaxWriteDetail(Date time,String instance){
        return getJson(time,instance,DISK_MAX_WRITE_DETAIL);
    }

    /**
     * 各主机最大磁盘输入（IO）详情
     * @return
     */
    public JSON getDiskMaxInputDetail(Date time,String instance){
        return getJson(time,instance,DISK_MAX_INPUT_DETAIL);
    }

    /**
     * 各主机最大磁盘输出（IO）详情
     * @return
     */
    public JSON getDiskMaxOutputDetail(Date time,String instance){
        return getJson(time,instance,DISK_MAX_OUTPUT_DETAIL);
    }

    /**
     * cpu使用率
     * @return
     */
    public JSON getSingleCpuUseRate(Date time,String instance){
        return getJson(time,instance,SINGLE_CPU_USE_RATE);
    }

    /**
     * 内存使用率
     * @return
     */
    public JSON getSingleMemoryUseRate(Date time,String instance){
        return getJson(time,instance,SINGLE_MEMORY_USE_RATE);
    }

    /**
     * CPU核数
     * @return
     */
    public JSON getSingleCpuNum(Date time,String instance){
        return getJson(time,instance,SINGLE_CPU_NUM);
    }

    /**
     * 总内存
     * @return
     */
    public JSON getSingleMemoryTotal(Date time,String instance){
        return getJson(time,instance,SINGLE_MEMORY_TOTAL);
    }

    /**
     * 启动时长
     * @return
     */
    public JSON getSingleStartUpTime(Date time,String instance){
        return getJson(time,instance,SINGLE_START_UP_TIME);
    }

    /**
     * 各分区cpu使用率
     * @return
     */
    public JSON getEachDiskUseRate(Date time,String instance){
        return getJson(time,instance,EACH_DISK_USE_RATE);
    }

    /**
     * 服务状态
     * @return
     */
    public JSON getServiceStatus(Date time,String instance){
        return getJson(time,instance,SERVICE_STATUS);
    }

    /**
     * 进程数
     * @return
     */
    public JSON getProcessNum(Date time,String instance){
        return getJson(time,instance,PROCESS_NUM);
    }

    /**
     * cpu使用率
     * @return
     */
    public JSON getSingleCpuUseRate2(Date time,String instance){
        return getJson(time,instance,SINGLE_CPU_USE_RATE2);
    }

    /**
     * 总物理内存
     * @return
     */
    public JSON getSinglePhysicalMemory(Date time,String instance){
        return getJson(time,instance,SINGLE_PHYSICAL_MEMORY);
    }

    /**
     * 剩余物理内存
     * @return
     */
    public JSON getSinglePhysicalMemoryFree(Date time,String instance){
        return getJson(time,instance,SINGLE_PHYSICAL_MEMORY_FREE);
    }

    /**
     * 总虚拟内存
     * @return
     */
    public JSON getSingleVirtualMemory(Date time,String instance){
        return getJson(time,instance,SINGLE_VIRTUAL_MEMORY);
    }

    /**
     * 剩余虚拟内存
     * @return
     */
    public JSON getSingleVirtualMemoryFree(Date time,String instance){
        return getJson(time,instance,SINGLE_VIRTUAL_MEMORY_FREE);
    }

    /**
     * 磁盘总空间
     * @return
     */
    public JSON getSingleDiskBytes(Date time,String instance){
        return getJson(time,instance,SINGLE_DISK_BYTES);
    }

    /**
     * 剩余磁盘空间
     * @return
     */
    public JSON getSingleDiskBytesFree(Date time,String instance){
        return getJson(time,instance,SINGLE_DISK_BYTES_FREE);
    }

    /**
     * 网络上传
     * @return
     */
    public JSON getSingleNetSent(Date time,String instance){
        return getJson(time,instance,SINGLE_NET_SENT);
    }

    /**
     * 网络下载
     * @return
     */
    public JSON getSingleNetReceived(Date time,String instance){
        return getJson(time,instance,SINGLE_NET_RECEIVED);
    }

    /**
     * 磁盘读
     * @return
     */
    public JSON getSingleDiskRead(Date time,String instance){
        return getJson(time,instance,SINGLE_DISK_READ);
    }

    /**
     * 磁盘写
     * @return
     */
    public JSON getSingleDiskWrite(Date time,String instance){
        return getJson(time,instance,SINGLE_DISK_WRITE);
    }

    /**
     * 磁盘input
     * @return
     */
    public JSON getSingleDiskInput(Date time,String instance){
        return getJson(time,instance,SINGLE_DISK_INPUT);
    }

    /**
     * 磁盘output
     * @return
     */
    public JSON getSingleDiskOutput(Date time,String instance){
        return getJson(time,instance,SINGLE_DISK_OUTPUT);
    }

    /**
     * 网络使用率
     * @return
     */
    public JSON getSingleNetUseRate(Date time,String instance){
        return getJson(time,instance,SINGLE_NET_USE_RATE);
    }

    /**
     * 网络发送丢包率
     * @return
     */
    public JSON getSingleNetSentErrorRate(Date time,String instance){
        return getJson(time,instance,SINGLE_NET_SENT_ERROR_RATE);
    }

    /**
     * 网络接受丢包率
     * @return
     */
    public JSON getSingleNetReceivedErrorRate(Date time,String instance){
        return getJson(time,instance,SINGLE_NET_RECEIVED_ERROR_RATE);
    }

    /**
     * 系统线程数
     * @return
     */
    public JSON getSingleThreadNum(Date time,String instance){
        return getJson(time,instance,SINGLE_THREAD_NUM);
    }

    /**
     * 系统异常调度
     * @return
     */
    public JSON getSingleSystemExceptionDispatches(Date time,String instance){
        return getJson(time,instance,SINGLE_SYSTEM_EXCEPTION_DISPATCHES);
    }

    /**
     * 判断是否替换application和instance
     * @param time 时间
     * @param instance windows_exporter IP和端口
     * @param query 原promQL语句
     * @return
     */
    private static JSON getJson(Date time, String instance, String query) {
        String t = PromQLUtil.parseDateToTime(time);
        promQL = query;
        if (StrUtil.isNotBlank(instance)){
            promQL = promQL.replace(WINDOWS_INSTANCE,instance);
        }
        String executePromQl = API_PREFIX + INSTANT_QUERY_PREFIX+ "query=" + promQL + "&time="+t;
        return PromQLUtil.executePromQl(executePromQl);
    }


    

}
